﻿--[[
    @brief    微信二维码扫描登录逻辑处理
    @Author    jameszhai
    @date    2017-11-22
--]]

EWeChatQRView = 
{
    sQRPanel = "pnlWeChatQRCode";
    sQRTips  = "rtbTips";

    sQRImage = "btnQRPict\\pnlQRPict";
    sQRMask  = "btnQRPict\\pnlQRMask";
    sBtnBack = "btnBack";
    sQRScanSucc = "btnQRPict\\pnlScanQRcodeSucc";
};

EWeChatQRTipText = 
{
    sScanTips = "<f c=\"0x656364\" al=\"center\">"..LoadStr("WECHAT_QRCODE_SCAN").."</f>";
    sTimeout  = "<f c=\"0x656364\" al=\"center\">"..LoadStr("WECHAT_QRCODE_QRCODE_INVAILD").."<f c=\"0x35a3d2\"><a>"..LoadStr("WECHAT_QRCODE_REFRESH").."</a></f></f>";
    sGetQRFail  = "<f c=\"0x656364\" al=\"center\">"..LoadStr("WECHAT_QRCODE_QRCODE_GET_FAILED").."<f c=\"0x35a3d2\"><a>"..LoadStr("WECHAT_QRCODE_REFRESH").."</a></f></f>";
    sWaitingConfirm = "<f c=\"0x656364\" al=\"center\">"..LoadStr("WECHAT_QRCODE_USE_MBOILEPHONE").."</f>";
};

TWeChatQRLogin =
{
    InitView = function(nRootID)
        TWeChatQRLogin.nRootID = nRootID;
        TWeChatQRLogin.bLoadOk = false;
    end;

    UnInitView = function()
        TWeChatQRLogin.nRootID  = 0;
        TWeChatQRLogin.nQRPanelID = 0;
        TWeChatQRLogin.pQRView  = nil;
        TWeChatQRLogin.pQRImage = nil;
        TWeChatQRLogin.pQRTips  = nil;
    end;

    CheckUI = function()
        if nil ~= TWeChatQRLogin.pQRView then
            return true;
        end;

        TWeChatQRLogin.pQRView = ui_utility.get_ctrl_ptr_by_path(
                                TWeChatQRLogin.nRootID, EWeChatQRView.sQRPanel);

        if nil == TWeChatQRLogin.pQRView then
            TGPLog.LogE("QRView", "Get qr panel fail.");
            return false;
        end;

        local     nTempID = TWeChatQRLogin.pQRView:GetControlID();

        TWeChatQRLogin.pQRTips  =    ui_utility.get_ctrl_ptr_by_path(nTempID, EWeChatQRView.sQRTips);
        if nil == TWeChatQRLogin.pQRView then
            TGPLog.LogE("QRView", "Get qr panel fail.");
            return false;
        end;

        TWeChatQRLogin.nQRPanelID = nTempID;

        ui_utility.setproperty_by_id(TWeChatQRLogin.pQRTips:GetControlID(),
                                      "OnRTBClickHyperLink", "TWeChatQRLogin.OnClkLink");

        TWeChatQRLogin.pQRMask  = ui_utility.get_ctrl_ptr_by_path(nTempID, EWeChatQRView.sQRMask);
        if nil == TWeChatQRLogin.pQRMask then
            TGPLog.LogE("QRView", "Get qr mask fail.");
            return false;
        end;

        TWeChatQRLogin.pQRImage = ui_utility.get_ctrl_ptr_by_path(nTempID, EWeChatQRView.sQRImage);
                if nil == TWeChatQRLogin.pQRImage then
            TGPLog.LogE("QRView", "Get qr image fail.");
            return false;
        end;

        ui_utility.setproperty_by_path(nTempID, "btnQRPict", "OnClick", "TWeChatQRLogin.OnClkQRImage");

        TWeChatQRLogin.pQRScanSucc = ui_utility.get_ctrl_ptr_by_path(nTempID, EWeChatQRView.sQRScanSucc);
        if TWeChatQRLogin.pQRScanSucc ~= nil then
            TWeChatQRLogin.pQRScanSucc:Show(FALSE);
        end

        TWeChatQRLogin.pQRMask:Show(FALSE);
        return true;
    end;

    ShowView = function()
        if TLoginView ~= nil then
            TLoginView.ShowCapsTips(false);
        end;

        if TWeChatQRLogin.CheckUI() then
            TWeChatQRLogin.pQRView:Show(TRUE);
        else
            TGPLog.LogE("QRLogin", "Check UI fail.");
        end;
    end;

    HideView = function()
        if nil ~= TWeChatQRLogin.pQRView then
            TWeChatQRLogin.pQRView:Show(FALSE);
        end;
    end;

    -- 设置状态
    SetStatus = function(zParam)
    end;

    ChangeImage = function(sImage)
        if nil ~= TWeChatQRLogin.pQRImage then
            sImage = base_utility.get_work_path() .. "\\tpf_ui\\res\\" .. sImage;

            local     sTemp = "ONEIMAGE:".. sImage ..";Stretch;0;"

            ui_utility.setproperty_by_id(TWeChatQRLogin.pQRImage:GetControlID(), 
                                         "Background", sTemp);
            ui_utility.setproperty_by_path(TWeChatQRLogin.nQRPanelID, "btnQRPict", "Background", "COLOR:RGB:255,255,255");
        end;

        if nil ~= TWeChatQRLogin.pQRMask then
            TWeChatQRLogin.pQRMask:Show(FALSE);
        end;
        if TWeChatQRLogin.pQRScanSucc ~= nil then
            TWeChatQRLogin.pQRScanSucc:Show(FALSE);
        end

    end;

    -- 显示QR图片
    ShowQRImage = function()
        TGPLog.LogI("QRLogin", "Show QR image.");
        TWeChatQRLogin.ShowQRTips(EWeChatQRTipText.sScanTips);
        TWeChatQRLogin.ChangeImage("login\\we_chat_qrcode.png");
    end;

    ShowGetQRFail = function()
        TGPLog.LogI("QRLogin", "Show QR fail.");
        TWeChatQRLogin.ShowQRTips(EWeChatQRTipText.sGetQRFail);
        TWeChatQRLogin.bQRCodeInvalid = true;
        if nil ~= TWeChatQRLogin.pQRImage then
            local     sTemp = "ONEIMAGE:client_login\\qrcode_fail.png;Stretch;0;"
            ui_utility.setproperty_by_id(TWeChatQRLogin.pQRImage:GetControlID(), 
                                         "Background", sTemp);
            ui_utility.setproperty_by_path(TWeChatQRLogin.nQRPanelID, "btnQRPict", "Background", "COLOR:RGB:255,255,255");
        end;

        if nil ~= TWeChatQRLogin.pQRMask then
            TWeChatQRLogin.pQRMask:Show(TRUE);
        end;
        if TWeChatQRLogin.pQRScanSucc ~= nil then
            TWeChatQRLogin.pQRScanSucc:Show(FALSE);
        end
    end;

    ShowQRScanedWaitingConfirm = function()
        TGPLog.LogI("QRLogin", "Show QR confirm.");
        TWeChatQRLogin.ShowQRTips(EWeChatQRTipText.sWaitingConfirm);
        if TWeChatQRLogin.pQRScanSucc ~= nil then
            TWeChatQRLogin.pQRScanSucc:Show(TRUE);
        end
        if nil ~= TWeChatQRLogin.pQRMask then
            TWeChatQRLogin.pQRMask:Show(TRUE);
        end;
    end;

    ShowQRTimeOut = function()
        TWeChatQRLogin.bQRCodeInvalid = true;

        TGPLog.LogI("QRLogin", "Show QR timeout.");
        TWeChatQRLogin.ShowQRTips(EWeChatQRTipText.sTimeout);

        if nil ~= TWeChatQRLogin.pQRMask then
            TWeChatQRLogin.pQRMask:Show(TRUE);
        end;
        if TWeChatQRLogin.pQRScanSucc ~= nil then
            TWeChatQRLogin.pQRScanSucc:Show(FALSE);
        end
    end;

    ShowRQChecking = function()
        TGPLog.LogI("QRLogin", "Show QR checking.");
        TWeChatQRLogin.ShowQRTips(EWeChatQRTipText.sScanTips);
        TWeChatQRLogin.ChangeImage("login\\qrcode_check.png");
    end;

    ShowQRTips = function(sTips)
        if nil ~= TWeChatQRLogin.pQRTips then
            local     sTemp = U8toCUIStr(sTips);
            TWeChatQRLogin.pQRTips:Clear();
            TWeChatQRLogin.pQRTips:PushBackHtmlString(sTemp);
        end;
    end;

    -- 刷新QRCode
    OnClkQRImage = function(pUILib, nBtnID)
        if TWeChatQRLogin.bQRCodeInvalid then
            TWeChatQRLogin.bQRCodeInvalid = false;
            TLoginCtrl.OnGetWeChatQRcode();
        end;
    end;

    -- 点击链接
    OnClkLink = function(pUILib, nCtrlID, rtObjt, nLink, sText, nX, nY, 
                         cKey, bCtrl, bShift, bAlt, sLink)
        TWeChatQRLogin.OnClkQRImage(pUILib, nCtrlID);
    end;

    nRootID  = 0;
    nQRPanelID = 0;

    pQRView  = nil;
    pQRImage = nil;
    pQRMask  = nil;
    pQRTips  = nil;
    pQRScanSucc = nil;

    bQRCodeInvalid = false;   -- 二维码超时或者失效

};